package org.jeecg.modules.room.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.room.entity.RoomInfo;
import org.jeecg.modules.room.entity.RoomStatus;
import org.jeecg.modules.room.mapper.RoomInfoMapper;
import org.jeecg.modules.room.service.IRoomInfoService;
import org.jeecg.modules.room.service.IRoomStatusService;
import org.jeecg.modules.room.vo.RoomCheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 房间信息
 * @Author: jeecg-boot
 * @Date:   2023-12-01
 * @Version: V1.0
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements IRoomInfoService {

    @Autowired
    private IRoomStatusService roomStatusService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInfo(RoomInfo roomInfo) {
        try {
            this.baseMapper.insert(roomInfo);
            RoomStatus roomStatus = new RoomStatus();
            roomStatus.setRoomCode(roomInfo.getRoomCode());
            roomStatus.setRoomState("VC");
            roomStatusService.save(roomStatus);
        }
        catch (Exception e){
            throw e;
        }
    }

    /**
     * 查询可用房间号
     * @param roomTypeCode
     * @param dateArrival
     * @param dateDeparture
     * @return
     */
    @Override
    public IPage<RoomInfo> selectUsableRoomList(IPage<RoomInfo> page, String roomTypeCode, String roomCode, String roomLevelCode, String isSelfUse, String roomDesc, String dateArrival, String dateDeparture) {

        IPage<RoomInfo> roomInfoList= this.baseMapper.selectUsableRoomList(page,roomTypeCode,roomCode,roomLevelCode,isSelfUse,roomDesc,dateArrival,dateDeparture);
        return roomInfoList;
    }

    /**
     * 查询可用房间数
     * @param roomTypeCode
     * @param dateArrival
     * @param dateDeparture
     * @return
     */
    @Override
    public Integer selectUsableRoomNum(String roomTypeCode,String dateArrival,String dateDeparture) {

        Map map = new HashMap<String,Object>();
        map.put("roomTypeCode",roomTypeCode);
        map.put("dateArrival",dateArrival);
        map.put("dateDeparture",dateDeparture);
        RoomCheckVo roomCheckVo = this.baseMapper.selectUsableRoomNum(map);

        //总的房间数- 维修 - 自用房数- 在住房 - 明确预定 + 允许超定数 - 未明确预定
        int usableNum = roomCheckVo.getTotalNum()-roomCheckVo.getHuNum()-roomCheckVo.getZdNum()-roomCheckVo.getYdNum()-roomCheckVo.getYtNum() + roomCheckVo.getOverNum();
        usableNum = usableNum - roomCheckVo.getUnYdNum() - roomCheckVo.getUnYtNum();
        return usableNum;
    }

}
